﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Arkanoide
{
    class Bola
    {

        
        Texture2D texture;
        Vector2 position;
       /* public Vector2 Position
        {
            get
            {
                return position;
            }
            set
            {
                position = value;
            }
        }*/
        public Rectangle size;
        Vector2 vel;
        GameWindow janela;
        SoundEffect soundEffect;

        public Bola(GameWindow janela, Texture2D texture, Vector2 position, SoundEffect soundEffect)
        {
            this.texture = texture;
            this.position = position;
            this.janela = janela;
            this.vel = new Vector2 (3,3);
            this.size = texture.Bounds;
            this.soundEffect = soundEffect;


        }

        public void LoadContent()
        {
           
        }

        public void Update()
        {

            position += vel;

            //atualiza a posicao do retangulo da bola de acordo com  a sua posicao 
            this.size.X = (int)position.X;
            this.size.Y = (int)position.Y;    


            #region Manter na Janela

            if (position.Y <= 50)
            {
                vel.Y *= -1;
                soundEffect.Play();
            }
            if (position.Y >= janela.ClientBounds.Height - texture.Height)
            {
                vel.Y *= -1;
                soundEffect.Play();
            }

            if (position.X <= 0)
            {
                vel.X *= -1;
                soundEffect.Play();
            }

            if (position.X >= janela.ClientBounds.Width - texture.Width)
            {
                vel.X *= -1;
                soundEffect.Play();
            }
            #endregion
        }

        //Rebater a bola na palheta
        public void Colision(Rectangle jogador1/*, Rectangle jogador2*/)
        {
            if (this.size.Intersects(jogador1) /*|| this.size.Intersects(jogador2)*/)
            {
                //Muda a direção
                               
                int lugarColisao = (int) ((( (position.X - jogador1.X)/(jogador1.Width/2) )-1)*(3));

                switch (lugarColisao)
                {
                    case 0:
                        vel.X = 0;
                        
                        break;

                    case 1:
                        vel.X = 1;
                        break;

                    case 2:
                        vel.X = 2;
                        break;

                    case 3:
                        vel.X = 3;
                        break;

                    case -1:
                        vel.X = -1;
                        break;

                    case -2:
                        vel.X = -2;
                        break;

                    case -3:
                        vel.X = -3;
                        break;


                }
                Console.WriteLine("vel = "+vel.X);

                vel.Y = -3;
                //this.vel *= new Vector2(1, -1);
                this.soundEffect.Play();
            }
        }

        //rebater a bola no tijolo
        public void ColisionBrick( int ImpactPoint )
        {
            switch (ImpactPoint)
            {
                case 0:
                    vel.X = 0;

                    break;

                case 1:
                    vel.X = 1;
                    break;

                case 2:
                    vel.X = 2;
                    break;

                case 3:
                    vel.X = 3;
                    break;

                case -1:
                    vel.X = -1;
                    break;

                case -2:
                    vel.X = -2;
                    break;

                case -3:
                    vel.X = -3;
                    break;
            }
        }


        public void Draw(SpriteBatch spriteBatch)
        {
           spriteBatch.Draw(texture, position, Color.White);
        }

    }
}
